
import string
from code.data.User import User
from code.dao.user_management import search_user,add_user,delete_user, update_user

class UserBackend:
    @staticmethod
    def login(userid: string, password: string):
        ret = 0
        if userid is None:
            ret = 4001
        elif password is None:
            ret = 4002
        else:
            user = User(userID=userid, password=password)
            user_back_list = search_user(user, "userID", False)
            if user_back_list is None:
                ret = 4008
            else:
                user_back = user_back_list[0]
                if user.password == user_back.password:
                    if user_back.valid is True:
                        ret = user_back
                    else:
                        ret = 4009
                else:
                    ret = 4008
        return ret

    @staticmethod
    def register(user: User):
        ret = 0;
        if user.userID is None:
            ret = 4003
        elif user.name is None:
            ret = 4007
        elif len(user.name) > 20:
            ret = 4017
        elif user.userid_re() != 1:
            ret = user.userid_re()
        elif user.email is None:
            ret = 4005
        elif user.email_re() != 1:
            ret = user.email_re()
        elif user.password is None:
            ret = 4004
        elif user.password_re() != 1:
            ret = user.password_re()
        else:
            user.valid = False
            user.type = "Student"
            search_user_return = search_user(user, "usrID")
            if search_user_return is None:
                create_user_return = add_user(user)
                if create_user_return:
                    ret = 1
                else:
                    ret = 0
            else:
                ret = 4016
        return ret

    @staticmethod
    def all_register_request():
        ret = 0
        register_request_user = search_user(User(valid=False), "valid")
        if register_request_user is None:
            ret = 4010
        else:
            register_request_user.sort(key=lambda user: user.userID)
            ret = register_request_user
        return ret

    @staticmethod
    def all_user():
        ret = 0
        all_user_back = search_user(User(valid=True), "valid")
        if all_user_back is None:
            ret = 4011
        else:
            all_user_back.sort(key=lambda user: user.userID)
            ret = all_user_back
        return ret

    @staticmethod
    def delete_user_(user: User):
        ret = 0
        if user.userID is None:
            ret = 4012
            print("has no usrID to delete")
        else:
            ret = delete_user(user)
        return ret

    @staticmethod
    def active_user(user:User):
        ret = 0
        user.valid = True
        statu = update_user(user, "vaild")
        if statu == True:
            ret = 1
        return ret


if __name__ == "__main__":
    print("1:", UserBackend.register(User(userID="2019300003001", name="陈雪阳", password="123456fd", email="1596252493@qq.com")))
    print(" ")
    print("4013:", UserBackend.register(User(userID="201930000302", name="陈雪阳", password="123456fd", email="1596252493@qq.com")))
    print(" ")
    print("4014:", UserBackend.register(User(userID="2019300003002", name="陈雪阳", password="123456", email="1596252493@qq.com")))
    print(" ")
    print("4015:", UserBackend.register(User(userID="2019300003002", name="陈雪阳", password="123456fd", email="1596252493qq.com")))
    print(" ")
    print("4015:", UserBackend.register(User(userID="2019300003002", name="陈雪阳", password="123456fd", email="1596252493@qqcom")))
    print(" ")
    print("4015:", UserBackend.register(User(userID="2019300003002", name="陈雪阳", password="123456fd", email="1596252493@.com")))
    print(" ")
    """print("4001:", UserBackend.login(None, "2343"))
    print(" ")
    print("4002:", UserBackend.login("admin", None))
    print(" ")
    print("4008:", UserBackend.login("kqx", "54321").name)
    print(" ")
    print("2019300003080", UserBackend.login("2019300003080", "123456").userID)
    print(" ")
    print("--------------register--------------------")
    print(" ")
    print("0:",UserBackend.all_register_request()[2].userID, " ", UserBackend.all_register_request()[3].userID )


    print("--------------register_end--------------------")"""